<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
<!--            <legend>搜索信息</legend>-->
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
<!--                            <label class="layui-form-label">应用名称</label>-->
                            <div class="layui-input-inline">
                                <input type="text" name="appName" placeholder="应用名称" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
<!--                            <label class="layui-form-label">客户名称</label>-->
                            <div class="layui-input-inline">
                                <input type="text" name="custName" placeholder="客户名称" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
<!--                            <label class="layui-form-label">联系方式</label>-->
                            <div class="layui-input-inline">
                                <input type="text" name="custMobile" placeholder="联系方式" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbar">
            <div class="layui-btn-group">
                <button type="button" class="layui-btn layui-btn-primary layui-btn-sm" lay-event="add">
                    <i class="layui-icon">&#xe654;</i>
                </button>
                <button type="button" class="layui-btn layui-btn-primary layui-btn-sm" lay-event="delete">
                    <i class="layui-icon">&#xe640;</i>
                </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
        const host = `${window.location.protocol}//${window.location.host}`;
        $.ajaxSetup({
            headers: {'Access-Token': sessionStorage.getItem('token'), 'userId': sessionStorage.getItem("userId")}
        });
        table.render({
            title: '应用列表',
            elem: '#currentTableId',
            method: 'get',
            url: `${host}/app/page`,
            toolbar: '#toolbar',
            defaultToolbar: ['filter', 'exports', 'print'],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 80, title: '序号'},
                {field: 'appName', width: 180, title: '应用名称'},
                {field: 'custName', width: 180, title: '客户名称'},
                {field: 'custContact', width: 180, title: '客户联系人'},
                {field: 'custMobile', width: 180, title: '联系方式'},
                {field: 'organizationName', width: 180, title: '机构'},
                {field: 'remark', width: 180, title: '说明'},
                {title: '操作',fixed: 'right', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line',
            parseData: (rest) => {
                return {
                    "code": rest.code,
                    "msg" : rest.msg,
                    "count" : rest.data.total,
                    "data" : rest.data.records
                }
            },
            request: {
                pageName: 'current',
                limitName: 'size'
            }
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            //执行搜索重载
            table.reload('currentTableId', {
                page: { current: 1 },
                where: { 'searchParams' : result }
            }, 'data');
            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '新增应用',
                    btn: ['确定','关闭'],
                    type: 2,
                    shade: 0.2,
                    maxmin:false,
                    shadeClose: false,
                    area: ['80%', '80%'],
                    content: `add.html?timestamp=${new Date().getTime()}`,
                    yes : (index, layero) => {
                        let addData = $(layero).find('iframe')[0].contentWindow.callbackData();
                        $.post(`${host}/app/save`, addData, (rest) => {
                            if (rest && "00" === rest.code) {
                                layer.msg(rest.msg);
                                $(".layui-laypage-btn")[0].click();
                                layer.close(index);
                            } else {
                                layer.msg(rest.msg);
                            }
                        });
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                let checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '修改应用',
                    btn: ['确定','关闭'],
                    type: 2,
                    shade: 0.2,
                    maxmin:false,
                    shadeClose: false,
                    area: ['80%', '80%'],
                    content: 'edit.html',
                    success: (layero, index) => {
                        var body = layer.getChildFrame('body', index);
                        body.find('hidden[name="id"]').val(obj.data.id);
                        body.find('input[name="appName"]').val(obj.data.appName);
                        body.find('input[name="custName"]').val(obj.data.custName);
                        body.find('input[name="custContact"]').val(obj.data.custContact);
                        body.find('input[name="custMobile"]').val(obj.data.custMobile);
                        body.find('textarea[name="remark"]').val(obj.data.remark);
                    },
                    yes : (index, layero) => {
                        let editData = $(layero).find('iframe')[0].contentWindow.callbackdata();
                        $.post("/app/save", editData, (rest) => {
                            if (rest && "00" === rest.code) {
                                layer.msg(rest.msg);
                                $(".layui-laypage-btn")[0].click();
                                layer.close(index);
                            } else {
                                layer.msg(rest.msg);
                            }
                        });
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('是否确定删除', function (index) {
                    let id = obj.data.id;
                    $.post("/app/delete", {'id': id}, (rest) => {
                        layer.msg(rest.msg);
                        if (rest && "00" === rest.code) {
                            obj.del();
                        }
                    })
                    layer.close(index);
                });
            }
        });
    });
</script>

</body>
</html>